<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateOperationLogsTable extends Migrator
{
    /**
     * 创建操作日志表
     */
    public function change()
    {
        $table = $this->table('operation_logs', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_unicode_ci',
            'comment' => '操作日志表'
        ]);

        $table->addColumn('user_id', 'integer', [
            'null' => true,
            'comment' => '操作用户ID'
        ])
        ->addColumn('username', 'string', [
            'limit' => 50,
            'null' => true,
            'comment' => '操作用户名'
        ])
        ->addColumn('action', 'string', [
            'limit' => 100,
            'null' => false,
            'comment' => '操作动作'
        ])
        ->addColumn('module', 'string', [
            'limit' => 50,
            'null' => false,
            'comment' => '操作模块'
        ])
        ->addColumn('description', 'text', [
            'null' => true,
            'comment' => '操作描述'
        ])
        ->addColumn('request_method', 'string', [
            'limit' => 10,
            'null' => false,
            'comment' => '请求方法'
        ])
        ->addColumn('request_url', 'string', [
            'limit' => 500,
            'null' => false,
            'comment' => '请求URL'
        ])
        ->addColumn('request_params', 'text', [
            'null' => true,
            'comment' => '请求参数'
        ])
        ->addColumn('response_code', 'integer', [
            'null' => true,
            'comment' => '响应状态码'
        ])
        ->addColumn('response_message', 'string', [
            'limit' => 500,
            'null' => true,
            'comment' => '响应消息'
        ])
        ->addColumn('ip_address', 'string', [
            'limit' => 45,
            'null' => true,
            'comment' => 'IP地址'
        ])
        ->addColumn('user_agent', 'string', [
            'limit' => 500,
            'null' => true,
            'comment' => '用户代理'
        ])
        ->addColumn('execution_time', 'decimal', [
            'precision' => 10,
            'scale' => 3,
            'null' => true,
            'comment' => '执行时间(秒)'
        ])
        ->addColumn('status', 'enum', [
            'values' => ['success', 'error', 'warning'],
            'default' => 'success',
            'comment' => '操作状态'
        ])
        ->addColumn('created_at', 'datetime', [
            'null' => false,
            'comment' => '创建时间'
        ])
        ->addColumn('updated_at', 'datetime', [
            'null' => false,
            'comment' => '更新时间'
        ]);

        $table->addIndex(['user_id'], ['name' => 'idx_user_id']);
        $table->addIndex(['action'], ['name' => 'idx_action']);
        $table->addIndex(['module'], ['name' => 'idx_module']);
        $table->addIndex(['status'], ['name' => 'idx_status']);
        $table->addIndex(['created_at'], ['name' => 'idx_created_at']);

        $table->create();
    }
} 